/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Ad_PhimInternalFrame.java
 *
 * Created on 04-04-2012, 22:42:34
 */
package GUI;

import DAO.MySQLDataAccessHelper;
import DAO.PhimDAO;
import POJO.PhimPoJo;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author tran quoc thinh
 */
public class Ad_PhimInternalFrame extends javax.swing.JInternalFrame {

    /** Creates new form Ad_PhimInternalFrame */
    public Ad_PhimInternalFrame() {
        initComponents();
    }
    private int a=-1;
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialog1 = new javax.swing.JDialog();
        jFrame1 = new javax.swing.JFrame();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbphim = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jtxtTenPhim = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtxtGioiThieuPhim = new javax.swing.JTextArea();
        jbtnThem = new javax.swing.JButton();
        jBtnCapNhat = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jtxtTinhTrang = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jtxtMaPhim = new javax.swing.JTextField();
        jbtnxoa = new javax.swing.JButton();

        javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("Quản Lý Phim");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));
        jPanel1.setForeground(new java.awt.Color(204, 204, 204));

        jtbphim.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jtbphim.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtbphimMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtbphim);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 534, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(22, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(56, Short.MAX_VALUE))
        );

        jPanel2.setBackground(new java.awt.Color(204, 204, 204));

        jLabel1.setText("Tên Phim");

        jLabel3.setText("Giới Thiệu Phim");

        jtxtGioiThieuPhim.setColumns(20);
        jtxtGioiThieuPhim.setRows(5);
        jScrollPane2.setViewportView(jtxtGioiThieuPhim);

        jbtnThem.setText("Thêm");
        jbtnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnThemActionPerformed(evt);
            }
        });

        jBtnCapNhat.setText("Cập Nhật");
        jBtnCapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnCapNhatActionPerformed(evt);
            }
        });

        jLabel4.setText("TinhTrang");

        jLabel5.setText("Mã Phim");

        jbtnxoa.setText("Xóa");
        jbtnxoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnxoaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addContainerGap(508, Short.MAX_VALUE))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel1)
                    .addComponent(jLabel5))
                .addGap(10, 10, 10)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jtxtMaPhim, javax.swing.GroupLayout.DEFAULT_SIZE, 323, Short.MAX_VALUE)
                        .addGap(152, 152, 152))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jbtnThem, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28)
                        .addComponent(jbtnxoa, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(41, 41, 41)
                        .addComponent(jBtnCapNhat, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jtxtTinhTrang, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 323, Short.MAX_VALUE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 323, Short.MAX_VALUE)
                            .addComponent(jtxtTenPhim, javax.swing.GroupLayout.DEFAULT_SIZE, 323, Short.MAX_VALUE))
                        .addGap(152, 152, 152))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jtxtMaPhim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtxtTenPhim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(14, 14, 14)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(40, 40, 40)
                        .addComponent(jtxtTinhTrang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jbtnxoa, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
                                .addComponent(jBtnCapNhat, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE))
                            .addComponent(jbtnThem, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(62, 62, 62)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 76, Short.MAX_VALUE)
                        .addComponent(jLabel4)
                        .addGap(59, 59, 59))))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(34, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jbtnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnThemActionPerformed
// TODO add your handling code here:
        String tenphim=this.jtxtTenPhim.getText().trim();
        String gtphim=this.jtxtGioiThieuPhim.getText().trim();    
        int tinhtrang=Integer.valueOf(this.jtxtTinhTrang.getText().trim());
        String maphim=this.jtxtMaPhim.getText().trim();     
            
      
        PhimPoJo p=new PhimPoJo();
        p.setTenPhim(tenphim);
        p.setGioiThieu(gtphim);
        p.setMaPhim(maphim);      
        p.setTinhtrang(tinhtrang);
        int n=PhimDAO.ThemPhim(p);
        if(n==1){
            JOptionPane.showMessageDialog(null,"Thêm Phim","Thanh Cong",JOptionPane.INFORMATION_MESSAGE);
            LoadPhim();
        }else{
             JOptionPane.showMessageDialog(null,"Thêm Phim","Khong Thanh Cong",JOptionPane.INFORMATION_MESSAGE);
        }       
}//GEN-LAST:event_jbtnThemActionPerformed

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        // TODO add your handling code here:
       LoadPhim();
    }//GEN-LAST:event_formInternalFrameOpened

    private void jtbphimMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtbphimMouseClicked
        // TODO add your handling code here:
        a=this.jtbphim.getSelectedRow();
        this.jtxtMaPhim.setText(this.jtbphim.getValueAt(a, 0).toString());
        this.jtxtTenPhim.setText(this.jtbphim.getValueAt(a, 1).toString());
        this.jtxtTinhTrang.setText(this.jtbphim.getValueAt(a, 3).toString());
        this.jtxtGioiThieuPhim.setText(this.jtbphim.getValueAt(a, 2).toString());    
    }//GEN-LAST:event_jtbphimMouseClicked

    private void jBtnCapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnCapNhatActionPerformed
        // TODO add your handling code here:
        String tenphim=this.jtxtTenPhim.getText().trim();
        String gtphim=this.jtxtGioiThieuPhim.getText().trim();    
        int tinhtrang=Integer.valueOf(this.jtxtTinhTrang.getText().trim());
        String maphim=this.jtxtMaPhim.getText().trim();
              
        PhimPoJo p=new PhimPoJo();
        p.setTenPhim(tenphim);
        p.setGioiThieu(gtphim);
        p.setMaPhim(maphim);       
        p.setTinhtrang(tinhtrang);
        int n=PhimDAO.SuaPhim(p);
        if(n==1){
            JOptionPane.showMessageDialog(null,"Cập Nhật Phim","Thanh Cong",JOptionPane.INFORMATION_MESSAGE);
            LoadPhim();
        }else{
             JOptionPane.showMessageDialog(null,"Cập Nhật Phim","Khong Thanh Cong",JOptionPane.INFORMATION_MESSAGE);
        }      
    }//GEN-LAST:event_jBtnCapNhatActionPerformed

    private void jbtnxoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnxoaActionPerformed
        // TODO add your handling code here:
        if(a==-1){
             JOptionPane.showMessageDialog(null,"Thông Báo","Chưa Chọn",JOptionPane.INFORMATION_MESSAGE);
        }else{
            String maphim=this.jtbphim.getValueAt(a, 0).toString();
            int n=PhimDAO.XoaPhim(maphim);
            if(n==1){
                JOptionPane.showMessageDialog(null,"Xóa Phim","Thanh Cong",JOptionPane.INFORMATION_MESSAGE);
            LoadPhim();
            }else{
                 JOptionPane.showMessageDialog(null,"Xóa Phim","Khong Thanh Cong",JOptionPane.INFORMATION_MESSAGE);
            }      
        }
        
    }//GEN-LAST:event_jbtnxoaActionPerformed
    private void LoadPhim(){
        try{         
           ResultSet rs=PhimDAO.DSPhim();
           DefaultTableModel model =new DefaultTableModel();
           model.addColumn("MaPhim");
           model.addColumn("TenPhim");
           model.addColumn("GioiThieu");         
           model.addColumn("TinhTrang");           
           while(rs.next()){
               Object[] a={rs.getString("MaPhim"),rs.getString("TenPhim"),rs.getString("GioiThieu"),
              rs.getString("TinhTrang")};
               model.addRow(a);
           }
           this.jtbphim.setModel(model);        
        }catch(Exception ex){
            System.err.println(ex);
        }       
        MySQLDataAccessHelper helper=new MySQLDataAccessHelper();
        helper.Close();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnCapNhat;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton jbtnThem;
    private javax.swing.JButton jbtnxoa;
    private javax.swing.JTable jtbphim;
    private javax.swing.JTextArea jtxtGioiThieuPhim;
    private javax.swing.JTextField jtxtMaPhim;
    private javax.swing.JTextField jtxtTenPhim;
    private javax.swing.JTextField jtxtTinhTrang;
    // End of variables declaration//GEN-END:variables
}
